package com.qsmx.qingqiuticket.app.delegate;

import android.annotation.SuppressLint;
import android.content.Context;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.qsmx.core.delegates.AtmosDelegate;
import com.qsmx.core.util.log.AtmosLogger;
import com.qsmx.qingqiuticket.app.R;
import com.qsmx.qingqiuticket.app.databinding.DelegateUsbPrintTestBinding;
import com.qsmx.qingqiuticket.app.delegate.adapter.UsbPrintTestAdapter;
import com.qsmx.qingqiuticket.app.event.UsbEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

public class UsbPrintTestDelegate extends AtmosDelegate {
    private String mUsbNum;
    private DelegateUsbPrintTestBinding mBinding;
    private UsbPrintTestAdapter topAdapter;
    private List<String> mUsbList;

    UsbManager manager;

    @Override
    public Object setLayout() {
        return R.layout.delegate_usb_print_test;
    }

    public static UsbPrintTestDelegate create(String usbNum) {

        final Bundle args = new Bundle();
        args.putString("USB_NUM", usbNum);
        final UsbPrintTestDelegate delegate = new UsbPrintTestDelegate();
        delegate.setArguments(args);
        return delegate;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Bundle args = getArguments();
        if (args != null) {
            mUsbNum = args.getString("USB_NUM");
        }
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {

        mBinding = DelegateUsbPrintTestBinding.bind(rootView);

        mUsbList = new ArrayList<>();
        initRecycler();
        getUsbDeviceList();
    }

    private void initRecycler() {
        topAdapter = new UsbPrintTestAdapter(getContext());
        mBinding.rlvTop.setAdapter(topAdapter);
        mBinding.rlvTop.setLayoutManager(new LinearLayoutManager(getContext()));

        mBinding.rlvTop.setNestedScrollingEnabled(false);

        topAdapter.setonItemClickListener(new UsbPrintTestAdapter.OnItemClickListener() {
            @Override
            public void onClick(View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("usbName", mUsbList.get(position));
                bundle.putString("usbNum", mUsbNum);
                EventBus.getDefault().post(new UsbEvent(bundle));
                getSupportDelegate().pop();
            }
        });
    }

    /**
     * 检查USB设备的PID与VID
     *
     * @param dev
     * @return
     */
    boolean checkUsbDevicePidVid(UsbDevice dev) {
        int pid = dev.getProductId();
        int vid = dev.getVendorId();
        return (vid == 0x5a5a && pid == 0x8009);
    }

    /**
     * 获取USB设备列表
     */
    @SuppressLint("NotifyDataSetChanged")
    public void getUsbDeviceList() {
        manager = (UsbManager) getProxyActivity().getSystemService(Context.USB_SERVICE);
        HashMap<String, UsbDevice> devices = manager.getDeviceList();
        Iterator<UsbDevice> deviceIterator = devices.values().iterator();
        int count = devices.size();
        AtmosLogger.d("count " + count);
        if (count > 0) {
            while (deviceIterator.hasNext()) {
                UsbDevice device = deviceIterator.next();
                String devicename = device.getDeviceName();
                String id = device.getSerialNumber();
                if (checkUsbDevicePidVid(device)) {
                    mUsbList.add(devicename);
                }
            }
        } else {
            String noDevices = getResources().getText(R.string.none_usb_device).toString();
            AtmosLogger.d("noDevices " + noDevices);
            mUsbList.add(noDevices);
        }

        if (mUsbList != null && !mUsbList.isEmpty()) {
            topAdapter.setDate(mUsbList);
            topAdapter.notifyDataSetChanged();
        } else {
            AtmosLogger.d("mUsbList is null");
        }
    }
}
